﻿using System;
using System.Web;

using Microsoft.Health;
using Microsoft.Health.ItemTypes;
using Microsoft.Health.Web;

public partial class _Default : System.Web.UI.Page 
{
    /// <summary>
    /// The person info object
    /// </summary>
    private PersonInfo personInfo = null;

    /// <summary>
    /// As the page is not deriving from HealthServicePage the pre-load function of WebApplicationUtilities has to be called here
    /// </summary>
    /// <param name="eventArgs">event argument</param>
    protected override void OnPreLoad(EventArgs eventArgs)
    {
        try
        {
            string personInfoXml = HttpContext.Current.Request.Params[/*WebApplicationConfiguration.QueryStringToken*/"PersonInfo"];

            if (!String.IsNullOrEmpty(personInfoXml))
            {
                // Try loading the person information from Cookie
                this.personInfo = WebApplicationUtilities.LoadPersonInfoFromCookie(HttpContext.Current);
            }

            if (this.personInfo == null)
            {
                // Logon required is true here
                this.personInfo = WebApplicationUtilities.PageOnPreLoad(HttpContext.Current, true);
            }
        }
        catch (Exception exception)
        {
            UserName.Text = "Exception" + exception.ToString();
        }
    }

    /// <summary>
    /// The page load function just try accessing the record items
    /// </summary>
    /// <param name="sender">sender argument</param>
    /// <param name="eventArgs">event argument</param>
    protected void Page_Load(object sender, EventArgs eventArgs)
    {
        try
        {
            if (personInfo != null)
            {
                // We have authenticated successfully ... now read the data
                UserName.Text = this.personInfo.Name;
                HealthRecordItemCollection weightCollection = this.personInfo.SelectedRecord.GetItemsByType(Weight.TypeId);
            }
        }
        catch (Exception exception)
        {
            UserName.Text = "Exception" + exception.ToString();
        }
    }
}
